package com.ruoyi.goods.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.constant.GoodsConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.enterwarehouse.domain.TGoodsBigitImport;
import com.ruoyi.enterwarehouse.domain.TGoodsEnterImport;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseInfo;
import com.ruoyi.exitwarehouse.domain.WExitWarehouseInfo;
import com.ruoyi.goods.domain.TGoodsWeightSum;
import com.ruoyi.statistic.domain.WStatisticDimensionBigit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.TGoodsInfoMapper;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 货物信息Service业务层处理
 * 
 * @author wzy
 * @date 2024-08-25 16:58:34
 */
@Service
public class TGoodsInfoServiceImpl implements ITGoodsInfoService 
{
    @Autowired
    private TGoodsInfoMapper tGoodsInfoMapper;

    private static Logger logger = LoggerFactory.getLogger(TGoodsInfoServiceImpl.class);
    /**
     * 查询货物信息
     *
     * @param goodsId 货物信息主键
     * @return 货物信息
     */
    @Override
    public TGoodsInfo selectTGoodsInfoByGoodsId(Long goodsId)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByGoodsId(goodsId);
    }

    /**
     * 查询货物信息列表
     *
     * @param tGoodsInfo 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoList(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoList(tGoodsInfo);
    }

    /**
     * 新增货物信息
     *
     * @param tGoodsInfo 货物信息
     * @return 结果
     */
    @Override
    public int insertTGoodsInfo(TGoodsInfo tGoodsInfo)
    {
        tGoodsInfo.setCreateTime(DateUtils.getNowDate());
        return tGoodsInfoMapper.insertTGoodsInfo(tGoodsInfo);
    }

    /**
     * 修改货物信息
     *
     * @param tGoodsInfo 货物信息
     * @return 结果
     */
    @Override
    public int updateTGoodsInfo(TGoodsInfo tGoodsInfo)
    {
        tGoodsInfo.setUpdateTime(DateUtils.getNowDate());
        return tGoodsInfoMapper.updateTGoodsInfo(tGoodsInfo);
    }

    /**
     * 批量删除货物信息
     *
     * @param goodsIds 需要删除的货物信息主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsInfoByGoodsIds(Long[] goodsIds)
    {
        return tGoodsInfoMapper.deleteTGoodsInfoByGoodsIds(goodsIds);
    }

    /**
     * 删除货物信息信息
     *
     * @param goodsId 货物信息主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsInfoByGoodsId(Long goodsId)
    {
        return tGoodsInfoMapper.deleteTGoodsInfoByGoodsId(goodsId);
    }

    /**
     * 批量更新货物运单编号
     *
     * @param goodsIds 需要更新的数据主键集合
     * @param waybillNumber 运单编号
     * @return 结果
     */
    @Override
    public int updateTGoodsWayBillNumber(Long[] goodsIds, String waybillNumber)
    {
        return tGoodsInfoMapper.updateTGoodsWayBillNumber(goodsIds, waybillNumber);
    }

    /**
     * 批量更新货物运单编号
     *
     * @param field 需要更新的字段约定
     * @param goodsIds 需要更新的数据主键集合
     * @param Number 运单编号
     * @return 结果
     */
    @Override
    public int updateTGoodsInfoByGoodsIds(Integer field, Long[] goodsIds, String Number)
    {
        return tGoodsInfoMapper.updateTGoodsInfoByGoodsIds(field, goodsIds, Number);
    }

    /**
     * 批量更新货物入库相关字段
     *
     * @param goodsIds 需要更新的数据主键集合
     * @param enterWarehouseInfo 入库单信息
     * @return 结果
     */
    @Override
    public int updateTGoodsInfoRelEnterWarehouseFieldsByGoodsIds(Long[] goodsIds, WEnterWarehouseInfo enterWarehouseInfo)
    {
        return tGoodsInfoMapper.updateTGoodsInfoRelEnterWarehouseFieldsByGoodsIds(goodsIds, enterWarehouseInfo);
    }

    /**
     * 批量更新货物出库相关字段
     *
     * @param goodsIds 需要更新的数据主键集合
     * @param exitWarehouseNumber 出库单编号
     * @param exitWarehouseInfo 出库单信息
     * @param inventoryStatus 库存状态
     * @return 结果
     */
    @Override
    public int updateTGoodsInfoRelExitWarehouseFieldsByGoodsIds(Long[] goodsIds, String exitWarehouseNumber, WExitWarehouseInfo exitWarehouseInfo, String inventoryStatus)
    {
        return tGoodsInfoMapper.updateTGoodsInfoRelExitWarehouseFieldsByGoodsIds(goodsIds, exitWarehouseNumber, exitWarehouseInfo, inventoryStatus);
    }

    /**
     * 批量更新货物出库相关字段
     *
     * @param exitWarehouseIds 需要更新的数据主键集合
     * @param exitWarehouseInfo 出库单信息
     * @param inventoryStatus 库存状态
     * @return 结果
     */
    @Override
    public int updateTGoodsInfoRelExitWarehouseFieldsByExitWarehouseIds(Long[] exitWarehouseIds, WExitWarehouseInfo exitWarehouseInfo, String inventoryStatus)
    {
        return tGoodsInfoMapper.updateTGoodsInfoRelExitWarehouseFieldsByExitWarehouseIds(exitWarehouseIds, exitWarehouseInfo, inventoryStatus);
    }

    /**
     * 查询货物信息列表
     *
     * @param field 需要更新的字段约定
     * @param number 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoByNumber(Integer field, String number)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByNumber(field, number);
    }

    /**
     * 查询货物信息列表
     *
     * @param goodsTransferId 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoByTransferId(Long goodsTransferId)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByTransferId(goodsTransferId);
    }

    /**
     * 删除货物信息信息
     *
     * @param field 需要更新的字段约定
     * @param goodsId 货物信息主键
     * @return 结果
     */
    @Override
    public int removeTGoodsInfoByGoodsIds(Integer field, Long[] goodsId)
    {
        return tGoodsInfoMapper.removeTGoodsInfoByGoodsIds(field, goodsId);
    }

    /**
     * 批量清空货物信息中，出库相关字段
     *
     * @param goodsIds 货物信息主键
     * @param exitWarehouseNumber 出库单号
     * @param operator 操作者
     * @return 结果
     */
    @Override
    public int removeTGoodsExitWarehouseRelFieldByGoodsIds(Long[] goodsIds, String exitWarehouseNumber, String operator)
    {
        return tGoodsInfoMapper.removeTGoodsExitWarehouseRelFieldByGoodsIds(goodsIds, exitWarehouseNumber, operator);
    }
    /**
     * 计算货物总重量
     *
     * @param waybillNumber 货物信息主键
     * @return 结果
     */
    @Override
    public double calculateTotalWeightByWayBillNumber(String waybillNumber)
    {
        return tGoodsInfoMapper.calculateTotalWeightByWayBillNumber(waybillNumber);
    }

    /**
     * 批量修改货物信息
     *
     * @param goodsInfos 货物信息
     * @return 结果
     */
    @Override
    public int batchUpdateTGoodsTransferRelFields(List<TGoodsInfo> goodsInfos)
    {
        return tGoodsInfoMapper.batchUpdateTGoodsTransferRelFields(goodsInfos);
    }

    /**
     * 批量删除货物信息
     *
     * @param goodsIds 需要删除的货物信息主键
     * @param enterWarehouseNumber 入库单号
     * @return 结果
     */
    @Override
    public int deleteTGoodsInfoExtByGoodsIds(Long[] goodsIds, String enterWarehouseNumber)
    {
        return tGoodsInfoMapper.deleteTGoodsInfoExtByGoodsIds(goodsIds, enterWarehouseNumber);
    }

    /**
     * 批量物理删除货物信息
     *
     * @param field 字段约定：入库Id，0，出库Id，1，货转Id，2
     * @param Ids 入库主键集合，出库主键集合，货转主键集合，
     * @return 结果
     */
    @Override
    public int deleteTGoodsInfoByEnterWarehouseIds(Integer field, Long[] Ids)
    {
        return tGoodsInfoMapper.deleteTGoodsInfoByEnterWarehouseIds(field ,Ids);
    }

    /**
     * 物理删除货物信息
     *
     * @param field 字段约定：入库单号，0，出库单号，1，货转单号，2
     * @param number 入库单号，出库单号，货转单号
     * @return 结果
     */
    @Override
    public int deleteTGoodsInfoByNumberExt(Integer field, String number)
    {
        return tGoodsInfoMapper.deleteTGoodsInfoByNumberExt(field, number);
    }

    /**
     * 批量更新货物状态
     *
     * @param field 需要更新的字段约定
     * @param warehouseNumber 入库或出库编号
     * @param status 在库状态
     * @return 结果
     */
    @Override
    public int updateTGoodsInventoryStatusByWarehouseNumber(Integer field, String warehouseNumber, String status)
    {
        return tGoodsInfoMapper.updateTGoodsInventoryStatusByWarehouseNumber(field, warehouseNumber, status);
    }

    /**
     * 批量更新货物状态
     *
     * @param enterWarehouseNumber 入库单号
     * @param inventoryStatus 在库状态
     * @param confirmPerson 入库确认人
     * @param confirmTime 入库确认时间
     * @return 结果
     */
    @Override
    public int updateTGoodsConfirmRelFieldByEnterWarehouseNumber(String enterWarehouseNumber, String inventoryStatus, String confirmPerson, Date confirmTime)
    {
        return tGoodsInfoMapper.updateTGoodsConfirmRelFieldByEnterWarehouseNumber(enterWarehouseNumber, inventoryStatus, confirmPerson, confirmTime);
    }

    /**
     * 批量更新货物状态
     *
     * @param enterWarehouseInfo 入库单号
     * @param inventoryStatus 在库状态
     * @return 结果
     */
    @Override
    public int updateTGoodsConfirmRelFieldsByEnterWarehouseNumber(WEnterWarehouseInfo enterWarehouseInfo, String inventoryStatus)
    {
        return tGoodsInfoMapper.updateTGoodsConfirmRelFieldsByEnterWarehouseNumber(enterWarehouseInfo, inventoryStatus);
    }

    /**
     * 批量更新货物状态
     *
     * @param wExitWarehouseInfo 入库单号
     * @param inventoryStatus 在库状态
     * @return 结果
     */
    @Override
    public int updateTGoodsConfirmRelFieldsByExitWarehouseNumber(WExitWarehouseInfo wExitWarehouseInfo, String inventoryStatus)
    {
        return tGoodsInfoMapper.updateTGoodsConfirmRelFieldsByExitWarehouseNumber(wExitWarehouseInfo, inventoryStatus);
    }

    /**
     * 批量更新货物状态
     *
     *
     * @param goodsIds 需要更新的货物信息主键集合
     * @param status 在库状态
     * @return 结果
     */
    @Override
    public int updateTGoodsInventoryStatusByGoodsIds(List<Long> goodsIds, String status)
    {
        return tGoodsInfoMapper.updateTGoodsInventoryStatusByGoodsIds(goodsIds, status);
    }

    /**
     * 批量更新货物状态
     *
     *
     * @param goodsTransferIds 货转单号
     * @param status 在库状态
     * @return 结果
     */
    @Override
    public int updateTGoodsInventoryStatusByTransferIds(Long[] goodsTransferIds, String status)
    {
        return tGoodsInfoMapper.updateTGoodsInventoryStatusByTransferIds(goodsTransferIds, status);
    }

    /**
     * 查询货物信息列表，履历
     *
     * @param tGoodsInfo 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoTraceList(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoTraceList(tGoodsInfo);
    }

    /**
     * 导入货物数据
     *
     * @param goodsList 用户数据列表
     * @param enterWarehouseNumber 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importGoods(List<TGoodsEnterImport> goodsList, String enterWarehouseNumber, String operName)
    {
        if (StringUtils.isNull(goodsList) || goodsList.size() == 0)
        {
            throw new ServiceException("导入货物数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        Date currentDate = new Date();
        for (TGoodsEnterImport goods : goodsList)
        {
            try
            {
                goods.setWarehouseTotalLocate(goods.getLogisticsParkName() + goods.getWarehouseLocateName() + goods.getRegionLocateName() + goods.getGoodsLocateName());
                // 验证是否存在这个货物
                Integer result = tGoodsInfoMapper.selectTGoodsInfoCountByBundleNumber(goods.getBundleNumber());
                if (result == 0)
                {
                    goods.setCreateBy(operName);
                    goods.setRecordSource("PC");
                    goods.setInventoryStatus("入库中");
                    goods.setEnterWarehouseNumber(enterWarehouseNumber);
                    goods.setEnterWarehouseCustomer(goods.getGoodsRightCustomer());
                    goods.setEnterWarehousePerson(operName);
                    goods.setEnterWarehouseTime(currentDate);
                    goods.setGoodsRemark("导入创建");
                    goods.setCreateBy(operName);
                    goods.setCreateTime(currentDate);
//                    logger.debug("FFF " + "goods = " + goods);
                    tGoodsInfoMapper.insertTGoodsInfoExt(goods);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、货物 " + goods.getBundleNumber() + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                logger.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 导入货物数据
     *
     * @param goodsList 用户数据列表
     * @param enterWarehouseNumber 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importGoodsBigit(List<TGoodsBigitImport> goodsList, String enterWarehouseNumber, String goodsType, Date enterWarehouseTime, String goodsRightCustomer, String operName)
    {
        if (StringUtils.isNull(goodsList) || goodsList.size() == 0)
        {
            throw new ServiceException("导入货物数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        Date currentDate = new Date();
        double weight = 0.0;
        for (TGoodsBigitImport goods : goodsList)
        {
            try
            {
                // 验证是否存在这个货物
                Integer result = tGoodsInfoMapper.selectTGoodsInfoCountByBundleNumber(goods.getBundleNumber());
                if (result == 0)
                {
                    if(StringUtils.isNotEmpty(goods.getGoodsRightCustomer()))
                    {
                        goods.setGoodsType(goodsType);
                        goods.setGoodsRightCustomer(goods.getGoodsRightCustomer().substring(8));
                        goods.setSpecification(goods.getPlateThick() + "x" + goods.getPlateWide() + "x" + goods.getPlateLong());
                        //goods.setWarehouseTotalLocate(goods.getLogisticsParkName() + goods.getWarehouseLocateName() + goods.getRegionLocateName() + goods.getGoodsLocateName());
                        if(goods.getWeightMode().equals("理论计重")) {
                            weight = goods.getTheoryWeight();
                            goods.setTheoryWeight(goods.getWeight());
                            goods.setWeight(weight);
                        }
                        goods.setCreateBy(operName);
                        goods.setRecordSource("PC");
                        goods.setInventoryStatus("入库中");
                        goods.setEnterWarehouseNumber(enterWarehouseNumber);
                        goods.setEnterWarehouseCustomer(goods.getGoodsRightCustomer());
                        goods.setEnterWarehousePerson(operName);
                        goods.setEnterWarehouseTime(enterWarehouseTime);
                        goods.setGoodsRemark("导入创建");
                        goods.setCreateBy(operName);
                        goods.setCreateTime(currentDate);
                        //logger.debug("FFF " + "goods = " + goods);
                        tGoodsInfoMapper.insertTGoodsInfoBigit(goods);
                        successNum++;
                        //优化去掉成功的，数据量大，但对用户来讲，意义不大
                        //successMsg.append("<br/>" + successNum + "、货物 " + goods.getBundleNumber() + " 导入成功");
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                logger.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据。");
        }
        return successMsg.toString();
    }

    /**
     * 获取货物重量合计
     *
     * @param inventoryStatus 用户数据列表
     * @return 结果
     */
    @Override
    public List<TGoodsWeightSum> getWeightSumByInventoryStatus(List<String> inventoryStatus)
    {
        return tGoodsInfoMapper.getWeightSumByInventoryStatus(inventoryStatus);
    }

    /**
     * 获取货物重量合计
     *
     * @param tGoodsInfo 货物信息参数
     * @return 结果
     */
    @Override
    public List<TGoodsWeightSum> getWeightSumByGoodsInfo(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.getWeightSumByGoodsInfo(tGoodsInfo);
    }

    /**
     * 获取统计维度
     *
     * @param begin 日期
     * @param end 日期
     * @return 结果
     */
    public List<WStatisticDimensionBigit> selectStatisticDimensionByEnterWarehouseDate(Date begin, Date end)
    {
        return tGoodsInfoMapper.selectStatisticDimensionByEnterWarehouseDate(begin, end);
    }

    /**
     * 查询货物信息列表，用于入库量统计
     *
     * @param tGoodsInfo 货物信息
     * @param begin 日期
     * @param end 日期
     * @return 货物信息
     */
    public List<TGoodsInfo> selectTGoodsInfoListUsedByEnterStatistic(TGoodsInfo tGoodsInfo, Date begin, Date end)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListUsedByEnterStatistic(tGoodsInfo, begin, end);
    }

    /**
     * 查询统计字段值
     *
     * @param begin 日期
     * @param end 日期
     * @return 结果
     */
    @Override
    public List<WStatisticDimensionBigit> selectStatisticDimensionByExitWarehouseDate(Date begin, Date end)
    {
        return tGoodsInfoMapper.selectStatisticDimensionByExitWarehouseDate(begin, end);
    }

    /**
     * 查询货物信息列表，用于日出库/入库量统计
     *
     * @param tGoodsInfo 货物信息
     * @param begin 日期
     * @param end 日期
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoListUsedByExitStatistic(TGoodsInfo tGoodsInfo, Date begin, Date end)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListUsedByExitStatistic(tGoodsInfo, begin, end);
    }

    /**
     * 查询货物信息列表，用于月出库/入库量统计
     *
     * @param field 条件字段约定，0，合约号，
     * @param tGoodsInfo 货物信息
     * @param begin 日期
     * @param end 日期
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoListUsedByMonthStatistic(Integer field, TGoodsInfo tGoodsInfo, Date begin, Date end)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListUsedByMonthStatistic(field, tGoodsInfo, begin, end);
    }

    /**
     * 查询货物信息列表，日出库量统计
     *
     * @param dayExitStatisticId 日出库统计ID
     * @param tGoodsInfo 货物信息参数
     * @return 货物信息集合
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoByDayExitStatisticId(Long dayExitStatisticId, TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByDayExitStatisticId(dayExitStatisticId, tGoodsInfo);
    }

    /**
     * 查询货物信息列表，月出库量统计
     *
     * @param monthExitStatisticId 月出库统计ID
     * @param tGoodsInfo 货物信息参数
     * @return 货物信息集合
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoByMonthExitStatisticId(Long monthExitStatisticId, TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByMonthExitStatisticId(monthExitStatisticId, tGoodsInfo);
    }

    /**
     * 理货修改库位信息
     *
     * @param goodsIds 货物主键ID
     * @param tGoodsInfo 货物信息参数
     * @return 货物信息集合
     */
    @Override
    public int tallyTGoodsInfoByGoodsIds(Long[] goodsIds, TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.tallyTGoodsInfoByGoodsIds(goodsIds, tGoodsInfo);
    }

    /**
     * 查询货物信息列表
     *
     * @param tGoodsInfo 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoListForMiniProgram(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListForMiniProgram(tGoodsInfo);
    }

    /**
     * 查询货物信息列表，移动端模糊查询，捆包号
     *
     * @param tGoodsInfo 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoListExt(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListExt(tGoodsInfo);
    }

    /**
     * 拆分货物
     *
     * @param numberOfSplit 拆分件数
     * @param splitWeightExt 拆分重量
     * @param tGoodsInfo 货物信息
     * @return 结果
     */
    @Override
    @Transactional
    public int splitGoodsInfoByNumberOfSplit(Integer numberOfSplit, Double splitWeightExt, TGoodsInfo tGoodsInfo)
    {
        String originBundle = tGoodsInfo.getBundleNumber();
        String[] bundleArray = originBundle.split("-");
        String bundleNubmer = bundleArray[0];
        Integer serialNumber = tGoodsInfoMapper.selectSerialNumberByBundleNumber(bundleNubmer);
        int rtn = 0;
        //logger.debug("FFF " + "serialNumber = " + serialNumber);

        TGoodsInfo goodsInfo = tGoodsInfoMapper.selectTGoodsInfoByGoodsId(tGoodsInfo.getGoodsId());
        if(StringUtils.isNotNull(goodsInfo)) {

            rtn = tGoodsInfoMapper.deleteTGoodsInfoByGoodsId(tGoodsInfo.getGoodsId());
            if (rtn < 1) {
                logger.error("FFF split goods delete fail ");
            }
            Double weight = tGoodsInfo.getWeight();
            //Double theoryWeight = tGoodsInfo.getTheoryWeight();
            Integer numberOfPiece = tGoodsInfo.getNumberOfPiece();
            Integer numberOfRemain = numberOfPiece - numberOfSplit;

            tGoodsInfo.setBundleNumber(bundleNubmer + "-" + serialNumber.toString());
            tGoodsInfo.setNumberOfPiece(numberOfSplit);
            Double weightSplit = weight * numberOfSplit / numberOfPiece;
            weightSplit = Math.round(weightSplit * 1000) / 1000.0;

            if (Double.compare(splitWeightExt, 0) != 0) {
                weightSplit = splitWeightExt;
            }
            tGoodsInfo.setWeight(weightSplit);

            //Double theoryWeightSplit = theoryWeight * numberOfSplit / numberOfPiece;
            //theoryWeightSplit = Math.round(theoryWeightSplit * 1000) / 1000.0;
            //tGoodsInfo.setTheoryWeight(theoryWeightSplit);
            tGoodsInfo.setGoodsRemark(originBundle);
            rtn = tGoodsInfoMapper.insertTGoodsInfo(tGoodsInfo);
            if (rtn < 1) {
                logger.error("FFF split goods insert fail ", tGoodsInfo.getBundleNumber());
            }

            serialNumber = serialNumber + 1;
            tGoodsInfo.setBundleNumber(bundleNubmer + "拆" + serialNumber.toString());
            tGoodsInfo.setNumberOfPiece(numberOfRemain);
            Double weightRemain = weight - weightSplit;
            weightRemain = Math.round(weightRemain * 1000) / 1000.0;
            tGoodsInfo.setWeight(weightRemain);
            //Double theoryWeightRemain = theoryWeight - theoryWeightSplit;
            //theoryWeightRemain = Math.round(theoryWeightRemain * 1000) / 1000.0;
            //tGoodsInfo.setTheoryWeight(theoryWeightRemain);
            tGoodsInfo.setGoodsRemark(originBundle);
            rtn = tGoodsInfoMapper.insertTGoodsInfo(tGoodsInfo);
            if (rtn < 1) {
                logger.error("FFF split goods insert fail ", tGoodsInfo.getBundleNumber());
            }
        }

        return rtn;
    }

    /**
     * 查询货物信息列表，盘库货物列表
     *
     * @param checkGoodsId 盘库ID
     * @param tGoodsInfo 货物信息参数
     * @return 货物信息集合
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoByCheckGoodsId(Long checkGoodsId, TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByCheckGoodsId(checkGoodsId, tGoodsInfo);
    }

    /**
     * 查询货物信息
     *
     * @param bundleNumber 货物信息主键
     * @return 货物信息
     */
    @Override
    public TGoodsInfo selectTGoodsInfoByBundleNumber(String bundleNumber)
    {
        return tGoodsInfoMapper.selectTGoodsInfoByBundleNumber(bundleNumber);
    }

    /**
     * 批量修改货物信息
     *
     * @param goodsInfos 货物信息
     * @return 结果
     */
    @Override
    public int batchUpdateTGoodsWarehouseFeeRelFields(List<TGoodsInfo> goodsInfos)
    {
        return tGoodsInfoMapper.batchUpdateTGoodsWarehouseFeeRelFields(goodsInfos);
    }

    /**
     * 查询货物计重模式
     *
     * @param contractNumber 合同号
     * @return 货物信息
     */
    @Override
    public String selectTGoodsWeightModeByContractNumber(String contractNumber)
    {
        return tGoodsInfoMapper.selectTGoodsWeightModeByContractNumber(contractNumber);
    }

    /**
     * 查询货物信息列表
     *
     * @param tGoodsInfo 货物信息
     * @return 货物信息
     */
    @Override
    public List<TGoodsInfo> selectTGoodsInfoListByMultipleBuddleNumber(TGoodsInfo tGoodsInfo)
    {
        return tGoodsInfoMapper.selectTGoodsInfoListByMultipleBuddleNumber(tGoodsInfo);
    }

    /**
     * 校验捆包号是否唯一
     *
     * @param tGoodsInfo 货物信息
     * @return 结果
     */
    @Override
    public String checkBundleNumberUnique(TGoodsInfo tGoodsInfo)
    {
        Long goodsId = StringUtils.isNull(tGoodsInfo.getGoodsId()) ? -1L : tGoodsInfo.getGoodsId();
        TGoodsInfo info = tGoodsInfoMapper.checkBundleNumberUnique(tGoodsInfo.getBundleNumber());
        if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
        {
            return GoodsConstants.NOT_UNIQUE;
        }
        return GoodsConstants.UNIQUE;
    }

    /**
     * 计算货物总重量
     *
     * @param field 字段约定，0，运单编号，1，入库编号，2，出库编号，
     * @param number 货物信息主键
     * @return 结果
     */
    @Override
    public double calculateGoodsTotalWeightByNumber(Integer field, String number)
    {
        return tGoodsInfoMapper.calculateGoodsTotalWeightByNumber(field, number);
    }

    /**
     * 查询货物信息，数量
     *
     * @param enterWarehouseNumber 入库单号
     * @return 货物信息
     */
    @Override
    public int selectTGoodsInfoCountByEnterWarehouseNumber(String enterWarehouseNumber)
    {
        return tGoodsInfoMapper.selectTGoodsInfoCountByEnterWarehouseNumber(enterWarehouseNumber);
    }

}
